package com.xy.smartsms.vista;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import com.xy.smartsms.data.SmsItem;
import com.xy.smartsms.presenter.BubblePresenter;
import com.xy.smartsms.presenter.BubblePresenterImpl;
import com.xy.smartsms.util.Log;

import java.util.Map;

/**
 * 提供卡片接入的视图和交互
 */
public class BubbleViewHolder extends AnimatorListenerAdapter implements BubbleVista {
    private final static String TAG = BubbleViewHolder.class.getSimpleName();
    public static final int MESSAGE_COMPLETE_ITEM = 0x100;
    public static final int MESSAGE_SHOW_DEFAULT = 0x101;
    /**
     * 简单气泡和原文的顶层资源ID
     */
    public static final String ID_TOP = "ID_TOP";
    /**
     * 丰富气泡的资源ID
     */
    public static final String ID_RICH = "ID_RICH";
    /**
     * URL安全校验的资源ID
     */
    public static final String ID_SAFE_URL = "ID_SAFE_URL";
    /**
     * 简单气泡的资源ID
     */
    public static final String ID_SIMPLE = "ID_SIMPLE";
    /**
     * 原文的资源ID
     */
    public static final String ID_CONTENT = "ID_CONTENT";
    /**
     * 简单气泡和丰富气泡切换的资源ID
     */
    public static final String ID_CHANGE = "ID_CHANGE";
    /**
     * 丰富气泡和简单气泡在卡片底部切换；默认关闭
     */
    public static final String FUNC_BOTTOM_CHANGE = "FUNC_BOTTOM_CHANGE";
    /**
     * 丰富气泡和简单气泡由卡片上的LOGO切换；默认关闭
     */
    public static final String FUNC_LOGO_CHANGE = "FUNC_LOGO_CHANGE";
    /**
     * 特征值；默认开启
     */
    public static final String FUNC_FEATURE = "FUNC_FEATURE";
    /**
     * 丰富气泡更多按钮点击事件监听；默认关闭
     */
    public static final String FUNC_RICH_MORE = "FUNC_RICH_MORE";

    private final static int ANIMATION_DIRECTION_RICH_TO_SIMPLE = 1;
    private final static int ANIMATION_DIRECTION_SIMPLE_TO_RICH = 2;

    private final static int CUSTOM_TYPE_MORE = 0; // BasePopupView.CALLBACK_TYPE_MORE

    private View mViewRoot = null;
    private View mViewParent = null;
    private Handler mHandler = null;
    private BubblePresenter mBubblePresenter = null;
    private int mCurrentBubbleMode = BubblePresenter.BUBBLE_MODE_RICH;
    private TextView mTextViewContent = null;
    private ViewGroup mViewGroupRich = null;
    private ViewGroup mViewGroupSimple = null;
    private ViewGroup mViewGroupSafeUrl = null;
    private View mViewTopContent = null;
    private View mViewChangeBubble = null;
    private Context mContext = null;
    private SmsItem mMessageItem = null;
    //private IXYSmartSmsItemHolder mItemHolder = null;
    private Map<String, String> mConfigMap = null;
    private boolean mFuncBottomChange = false;
    private boolean mFuncLogoChange = false;
    private boolean mFuncFeature = true;
    private boolean mFuncRichMore = false;

    public BubbleViewHolder(Context context, Handler handler, View rootView, View parentView) {
        mContext = context;
        mViewRoot = rootView;
        mViewParent = parentView;
        mHandler = handler;
    }

    /**
     * 配置资源ID以及功能选择
     *
     * @param map
     */
    public void setMap(Map<String, String> map) {
        mConfigMap = map;
        initView();
        initVariable();
    }

    private void initVariable() {
        mBubblePresenter = new BubblePresenterImpl(mContext);
        mBubblePresenter.setVista(this);
        mBubblePresenter.setAdapterView((ViewGroup) mViewParent);
    }


    /**
     * 根据指定的初始模式来绑定卡片
     *
     * @param item 信息接口
     * @param mode 显示模式，@seealso BubblePresenter.BUBBLE_MODE_PRIMITIVE
     */
    public void bind(SmsItem item, int mode) {
        if (item == null) {
            return;
        }

        mMessageItem = item;
        mBubblePresenter.setData(mMessageItem, false);
        initListItem();
        showByBubbleMode(mode);
    }

    /**
     * 卡片绑定
     *
     * @param item 信息接口
     */
    public void bind(SmsItem item) {
        bind(item, BubblePresenter.BUBBLE_MODE_RICH);
    }

    private void loadBubble(int type) {
        mBubblePresenter.load(type);
    }

    private void initView() {
        //mViewTopContent = mViewRoot.findViewById(Integer.valueOf(mConfigMap.get(ID_TOP)));
        mTextViewContent = (TextView) mViewRoot.findViewById(Integer.valueOf(mConfigMap.get(ID_CONTENT)));
        mViewTopContent = (View) mTextViewContent.getParent();

        String config;
        if ((config = mConfigMap.get(ID_RICH)) != null) {
            mViewGroupRich = (ViewGroup) mViewRoot.findViewById(Integer.valueOf(config));
        }

        if ((config = mConfigMap.get(ID_SIMPLE)) != null) {
            mViewGroupSimple = (ViewGroup) mViewRoot.findViewById(Integer.valueOf(config));
        }

        if ((config = mConfigMap.get(ID_SAFE_URL)) != null) {
            mViewGroupSafeUrl = (ViewGroup) mViewRoot.findViewById(Integer.valueOf(config));
        }

        if ((config = mConfigMap.get(ID_CHANGE)) != null) {
            mViewChangeBubble = mViewRoot.findViewById(Integer.valueOf(config));
            mViewChangeBubble.setOnClickListener(mChangeBubbleClickListener);
        }

        if ((config = mConfigMap.get(FUNC_BOTTOM_CHANGE)) != null) {
            mFuncBottomChange = Boolean.valueOf(config);
        }

        if ((config = mConfigMap.get(FUNC_LOGO_CHANGE)) != null) {
            mFuncLogoChange = Boolean.valueOf(config);
        }

        if ((config = mConfigMap.get(FUNC_FEATURE)) != null) {
            mFuncFeature = Boolean.valueOf(config);
        }

        if ((config = mConfigMap.get(FUNC_RICH_MORE)) != null) {
            mFuncRichMore = Boolean.valueOf(config);
        }
    }

    private View.OnClickListener mChangeBubbleClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switchBubble();
        }
    };

    private void switchBubble() {
        if (mIsAnimationRunning) {
            return;
        }

        if (mCurrentBubbleMode == BubblePresenter.BUBBLE_MODE_RICH) {
            if (isChangeSeparated()) {
                mViewChangeBubble.setVisibility(View.VISIBLE);
            }

            showNotRichBubble(true);
        } else {
            if (isChangeSeparated()) {
                mViewChangeBubble.setVisibility(View.GONE);
            }

            loadBubble(BubblePresenter.TYPE_RICH);
        }

        runCardAnimation((mCurrentBubbleMode == BubblePresenter.BUBBLE_MODE_RICH) ?
                ANIMATION_DIRECTION_RICH_TO_SIMPLE : ANIMATION_DIRECTION_SIMPLE_TO_RICH);
    }

    private void initListItem() {
        if (mViewChangeBubble != null) {
            mViewChangeBubble.setVisibility(View.GONE);
        }

        if (mViewGroupSafeUrl != null) {
            mViewGroupSafeUrl.setVisibility(View.GONE);
        }

        if (mViewGroupSimple != null) {
            mViewGroupSimple.setVisibility(View.GONE);
        }

        if (mViewGroupRich != null) {
            mViewGroupRich.setVisibility(View.GONE);
        }

        mViewTopContent.setVisibility(View.VISIBLE);
    }

    private void showByBubbleMode(int mode) {
        if (mode == BubblePresenter.BUBBLE_MODE_PRIMITIVE || mBubblePresenter.getBubbleMode() != BubblePresenter.BUBBLE_MODE_RICH) {
            mCurrentBubbleMode = BubblePresenter.BUBBLE_MODE_SIMPLE;
            if (mViewChangeBubble != null) {
                mViewChangeBubble.setVisibility(View.VISIBLE);
            }

            mViewTopContent.setVisibility(View.VISIBLE);

            showNotRichBubble(mode == BubblePresenter.BUBBLE_MODE_RICH);
        } else {
            mCurrentBubbleMode = BubblePresenter.BUBBLE_MODE_RICH;
            loadBubble(BubblePresenter.TYPE_RICH);
        }
    }

    private boolean isChangeSeparated() {
        return mFuncLogoChange && mViewChangeBubble != null;
    }

    private boolean mIsAnimationRunning = false;

    @Override
    public void onAnimationStart(Animator animation) {
        mIsAnimationRunning = true;
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (mCurrentBubbleMode == BubblePresenter.BUBBLE_MODE_RICH) {
            // current bubble is rich, so switch to content + simple
            mCurrentBubbleMode = BubblePresenter.BUBBLE_MODE_SIMPLE;
        } else {
            mCurrentBubbleMode = BubblePresenter.BUBBLE_MODE_RICH;
        }

        mBubblePresenter.setBubbleMode(mCurrentBubbleMode);
        ensureComplete();
        mIsAnimationRunning = false;
    }

    private void runCardAnimation(int direction) {
        //        if (direction == ANIMATION_DIRECTION_RICH_TO_SIMPLE) {
        //            CardAnimUtil.applyItemRotation(mViewGroupRich, mViewGroupRich, mViewTopContent, 200, 300, this);
        //        } else if (direction == ANIMATION_DIRECTION_SIMPLE_TO_RICH) {
        //            CardAnimUtil.applyItemRotation(mViewTopContent, mViewTopContent, mViewGroupRich, 200, 300, this);
        //        }
    }


    private void ensureComplete() {
        Message message = new Message();
        message.what = MESSAGE_COMPLETE_ITEM;
        message.obj = mViewRoot;
        mHandler.sendMessage(message);
    }

    // BubbleVista start
    private void removeViewFromParent(View v) {
        ViewParent parent = v.getParent();
        if (parent instanceof ViewGroup) {
            ViewGroup p = (ViewGroup) parent;
            p.removeView(v);
        }
    }

    private void showNotRichBubble(boolean hasBubble) {
        Message message = new Message();
        message.what = MESSAGE_SHOW_DEFAULT;
        message.obj = hasBubble;
        mHandler.sendMessage(message);
        // TODO
        //mItemHolder.showDefaultItem(hasBubble);

        if (mViewGroupSimple != null) {
            loadBubble(BubblePresenter.TYPE_SIMPLE);
        }

        if (mFuncFeature) {
            loadBubble(BubblePresenter.TYPE_FEATURE);
        }

        if (mViewGroupSafeUrl != null) {
            loadBubble(BubblePresenter.TYPE_SENSITIVE);
        }
    }

    @Override
    public void showCompoundBubble(View v) {
        if (v != null && mViewGroupRich != null) {
            mViewGroupRich.removeAllViews();
            removeViewFromParent(v);
            mViewGroupRich.addView(v);
            if (mCurrentBubbleMode == BubblePresenter.BUBBLE_MODE_RICH) {
                mViewGroupRich.setVisibility(View.VISIBLE);
                mViewTopContent.setVisibility(View.GONE);
            }
        } else {
            showNotRichBubble(false);
        }
    }

    @Override
    public void showRichBubble(View v) {
        if (v != null && mViewGroupRich != null) {
            mViewGroupRich.removeAllViews();
            removeViewFromParent(v);
            mViewGroupRich.addView(v);
            if (mCurrentBubbleMode == BubblePresenter.BUBBLE_MODE_RICH) {
                mViewGroupRich.setVisibility(View.VISIBLE);
                mViewTopContent.setVisibility(View.GONE);
            }
        } else {
            showNotRichBubble(false);
        }
    }

    @Override
    public void showSimpleBubble(View v) {
        if (v != null && mViewGroupSimple != null && mTextViewContent != null) {
            mViewGroupSimple.removeAllViews();
            removeViewFromParent(v);
            mViewGroupSimple.addView(v);
            mViewGroupSimple.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showFeature(CharSequence spanString) {
        if (mTextViewContent != null && spanString != null) {
            mTextViewContent.setText(spanString);
        }
    }

    @Override
    public void showSensitive(View v) {
        if (v != null && mViewGroupSafeUrl != null) {
            mViewGroupSafeUrl.removeAllViews();
            removeViewFromParent(v);
            mViewGroupSafeUrl.addView(v);
            mViewGroupSafeUrl.setVisibility(View.VISIBLE);
        }
    }
    // BubbleVista end

}
